//
//  ANNavigationVC.m
//  项目模板
//
//  Created by ZHM on 14/12/31.
//  Copyright © 2014年 ZHM. All rights reserved.
//

#import "ANNavigationVC.h"

#import "UIBarButtonItem+item.h"

@interface ANNavigationVC ()<UINavigationControllerDelegate>
@property (nonatomic,strong)id popDelegate;
@end

@implementation ANNavigationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.navigationBar.barStyle = UIBarStyleDefault;
//    self.navigationBar.translucent = NO;
//    self.navigationBar.tintColor = [UIColor orangeColor];
    //实现覆盖自带返回按钮后的返回功能
    _popDelegate = self.interactivePopGestureRecognizer.delegate;
    self.delegate = self;

}

+ (void)initialize {
    // 获取当前类的UIBarButtonItem
    //UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedIn:self, nil];
    NSMutableDictionary *attrib = [NSMutableDictionary dictionary];
    attrib[NSForegroundColorAttributeName] = [UIColor orangeColor];
    //设置导航条字体颜色
    [[UIBarButtonItem appearance] setTitleTextAttributes:attrib forState:UIControlStateNormal];
    
    NSMutableDictionary *attribDisabled = [NSMutableDictionary dictionary];
    attribDisabled[NSForegroundColorAttributeName] = [UIColor blackColor];
    [[UIBarButtonItem appearance] setTitleTextAttributes: attribDisabled
                                                forState: UIControlStateHighlighted];
    
    //不可用状态字体颜色  注意这样不起作用

//    [item setTitleTextAttributes:attribDisabled forState:UIControlStateHighlighted];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//导航控制器即将显示时候调用
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    //获取主窗口
    UITabBarController *tabBarVC =  (UITabBarController*)ANKeyWindow.rootViewController;
    //移除系统的TABBARBUTTON
    for (UIView *tabBarButton in tabBarVC.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBarButton removeFromSuperview];
        }
    }
}

//导航控制器跳转完成时候调用
#pragma mark --UINavigationControllerDelegate代理方法
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    //HMLog(@"%s",__func__);
    if (viewController == self.viewControllers[0]) {
        self.interactivePopGestureRecognizer.delegate = _popDelegate;
        
    }else {
        //清空滑动返回手势的代理.就能实现自定义滑动
        self.interactivePopGestureRecognizer.delegate = nil;
    }
}

//重写pushViewController
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [super pushViewController:viewController animated:animated];
    //判断是不是跟控制器
    if (self.viewControllers.count != 1) {
        // 设置导航条左右按钮
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"navigationbar_back_highlighted"] highImage:[UIImage imageNamed:@"navigationbar_back"] target:self action:@selector(backToVC) forControlEvents:UIControlEventTouchUpInside];
        viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"navigationbar_more_highlighted"] highImage:[UIImage imageNamed:@"navigationbar_more"] target:self action:@selector(backToRoot) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)backToVC {
    [self popViewControllerAnimated:YES];
}
- (void)backToRoot{
    [self popToRootViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
